﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

namespace ZoeEngine.Framework
{
    public abstract class EntityComponent : ITicked
    {
        private Entity _owner;
        private int _updateOrder;

        public virtual void Initialize()
        {
            object[] attributes = this.GetType().GetCustomAttributes(true);
            foreach (object o in attributes)
            {
                if (o is ComponentDependency)
                {
                    ComponentDependency cd = o as ComponentDependency;
                    foreach (Type t in cd.Dependencies)
                    {
                        if (!Owner.Contains(t))
                            throw new ArgumentException("Component dependency unsatisifed.");
                    }
                }
            }
        }
        public virtual void Start() { }
        public virtual void Update(GameTime gameTime) { }

        public event EventHandler UpdateOrderChanged;
        protected virtual void OnUpdateOrderChanged(EventArgs e)
        {
            if (this.UpdateOrderChanged != null)
            {
                this.UpdateOrderChanged(this, e);
            }
        }

        public event EventHandler OwnerChanged;
        protected virtual void OnOwnerChanged(EventArgs e)
        {
            if (this.OwnerChanged != null)
            {
                this.OwnerChanged(this, e);
            }
        }

        public Entity Owner
        {
            get
            {
                return _owner;
            }
            set
            {
                if (value != _owner)
                {
                    _owner = value;
                    OnOwnerChanged(EventArgs.Empty);
                }
            }
        }
        public int UpdateOrder
        {
            get
            {
                return _updateOrder;
            }
            set
            {
                if (_updateOrder != value)
                {
                    _updateOrder = value;
                    OnUpdateOrderChanged(EventArgs.Empty);
                }
            }
        }
    }
}
